<?php
/**
 * Created by PhpStorm.
 * User: zyark
 * Date: 2017/8/15
 * Time: 09:05
 */

namespace Admin\Controller;


use Admin\Logic\LotteryLgLogic;
use Admin\Logic\MatchLogic;
use Admin\Logic\OrderLogic;
use Admin\Logic\RoomLogic;
use Admin\Model\Page;
use Common\Utils\Constants;
use Home\Logic\MatchLogLogic;

class LogController extends BaseController
{
    /**
     * 查看用户资金流动
     */
    public function moneyLog(){
        $this->bcItemPush('用户资金流动', U('Log/moneyLog'));
        //显示数据
        $this->display();
    }

    public function ajaxMoneyLog(){
        $model = D('MoneyLog');
        //构建查询条件
        $pagesize = 20;
        $user_id = I('user_id');
        $type = I('type');
        $put_type = I('put_type');
        $money_type = I('money_type');
        $pagenumber = I('pagenum', '');
        $startTime = I('startTime', '');
        $endTime = I('endTime', '');
        if (empty($pagenumber) || $pagenumber <= 0)
            $pagenumber = 1;
        if ($user_id != '') {
            $map['t_money_log.user_id'] = $user_id;
        }
        if ($type != '' && $type != '-1') {
            $map['t_money_log.type'] = $type;
        }
        if ($put_type != '' && $put_type != '-1') {
            $map['t_money_log.put_type'] = $put_type;
        }
        if ($money_type != '' && $money_type != '-1') {
            $map['t_money_log.money_type'] = $money_type;
        }
        if ($startTime != null && $startTime != '' && $endTime != null && $endTime != '') {
            $map['t_money_log.create_time'] = array(array('gt', strtotime($startTime)), array('lt', strtotime($endTime)));
        }
        //查询
        $startno = ($pagenumber - 1) * $pagesize;
        $limit = $startno . ',' . $pagesize;//每页的数据数和内容$limit
        $list = $model->where($map)->order('id desc')->limit($limit)
            ->field('t_money_log.*')->select();
        foreach ($list as $key => $value) {
            $list[$key]['create_time'] = date("Y-m-d H:i:s", $list[$key]['create_time']);
        }
        //计算总金额
        $total_money = $model->where($map)->field('sum(money) as total_money')->select();
        $recordcount = $model->where($map)->count();
        //封装分页对象
        $page = new Page($pagenumber, $recordcount, $pagesize);
        $page = (array)$page;//转换为数组

        $this->assign('page', $page);
        $this->assign('list', $list);
        $this->assign('recordcount', $recordcount);
        $this->assign('total_money', $total_money[0]['total_money']);
        layout(false);
        $this->display();
    }

    public function matchLog(){
        $model = D('MatchLog');
        //构建查询条件
        $pagesize = 20;
        $user_id = I('user_id');
        $status = I('status');
        $winner = I('winner');
        $room_id = I('room_id');
        $opponent_id= I('opponent_id');
        $pagenumber = I('pagenum', '');
        $startTime = I('startTime', '');
        $endTime = I('endTime', '');
        $join_id = I('join_id');
        $model_type = I('model');
        if (empty($pagenumber) || $pagenumber <= 0)
            $pagenumber = 1;
        if(!empty($join_id)){
            $recommend_id = D('User')->where(array('id'=>$join_id))->getField('recommend_id');
            $user = D('User')->where(array('id'=>$recommend_id))->find();
            $this->assign('recommend_user',$user);
            $this->assign('show_recommend',1);
            $where['_string'] = ' t_match_log.user_id = '.$join_id.' OR'.' t_match_log.opponent_id = '.$join_id.'';
            $map['_complex'] = $where;
            $map['t_match_log.is_opponent_delete'] = array('neq',$join_id);
            $map['t_match_log.is_user_delete'] = array('neq',$join_id);
        }
        if ($user_id != '') {
            $map['t_match_log.user_id'] = $user_id;
        }
        if ($room_id != '') {
            $map['t_match_log.room_id'] = $room_id;
        }
        if ($status != '' && $status != '-1') {
            $map['t_match_log.status'] = $status;
        }
        if ($winner != '' && $winner != '-1') {
            $map['t_match_log.winner'] = $winner;
        }
        if ($winner != '' && $winner != '-1') {
            $map['t_match_log.winner'] = $winner;
        }
        if ($opponent_id != '' && $opponent_id != '-1') {
            $map['t_match_log.opponent_id'] = $opponent_id;
        }
        if ($model_type != '' && $model_type != '-1') {
            $map['t_match_log.model'] = $model_type;
        }
        if ($startTime != null && $startTime != '' && $endTime != null && $endTime != '') {
            $map['t_match_log.create_time'] = array(array('gt', strtotime($startTime)), array('lt', strtotime($endTime)));
        }
        $map['t_match_log.is_delete'] = 0;
        //查询
        $startno = ($pagenumber - 1) * $pagesize;
        $limit = $startno . ',' . $pagesize;//每页的数据数和内容$limit
        $list = $model->join('LEFT JOIN t_user u ON u.id=t_match_log.user_id')
            ->join('LEFT JOIN t_user ou ON ou.id=t_match_log.opponent_id')
            ->join('LEFT JOIN t_room r ON r.id=t_match_log.room_id')
            ->where($map)->order('id desc')->limit($limit)
            ->field('t_match_log.*,ou.wechat_number as o_wechat_number,u.wechat_number as wechat_number,
            ou.game_name as o_game_name,u.game_name as game_name,r.integral,r.money_type')->select();
//        var_dump($list);
        foreach ($list as $key => $value) {
            $list[$key]['create_time'] = date("Y-m-d H:i:s", $list[$key]['create_time']);
        }
        $recordcount = $model->where($map)->count();
        //封装分页对象
        $page = new Page($pagenumber, $recordcount, $pagesize);
        $page = (array)$page;//转换为数组

        $this->bcItemPush('比赛列表', U('Log/matchLog'));

        //显示数据
        $this->assign('startTime', $startTime);
        $this->assign('endTime', $endTime);
        $this->assign('page', $page);
        $this->assign('user_id', $user_id);
        $this->assign('join_id', $join_id);
        $this->assign('status', $status);
        $this->assign('winner', $winner);
        $this->assign('opponent_id', $opponent_id);
        $this->assign('model', $model_type);
        $this->assign('room_id', $room_id);
        $this->assign('pagenumber', $pagenumber);
        $this->assign('list', $list);
        $this->assign('recordcount', $recordcount);
        $this->display('matchLog');
    }

    /**
     * ajax判定输赢
     */
    public function judge(){
        $winner_user_id = I('winner_user_id');
        $log_id = I('log_id');
        if(empty($winner_user_id) || empty($log_id)){
            $this->ajaxReturn(array(Constants::SUCCESS=>false,Constants::MESSAGE=>'判定失败!'));
        }
        $logic = new MatchLogic();
        $status = $logic->judge($winner_user_id,$log_id);
        switch ($status){
            case 0:
                $this->ajaxReturn(array(Constants::SUCCESS=>false,Constants::MESSAGE=>'该记录已经处理过!'));
                break;
            case 1:
                $message = '判定成功！';
                $this->ajaxReturn(array(Constants::SUCCESS=>true,Constants::MESSAGE=>$message));
                break;
            case 2:
                $this->ajaxReturn(array(Constants::SUCCESS=>false,Constants::MESSAGE=>'判定失败!'));
                break;
            case 3:
                $this->ajaxReturn(array(Constants::SUCCESS=>false,Constants::MESSAGE=>'获胜者不在该比赛中!'));
                break;
        }
    }

    /**
     * ajax审核房间解散记录
     */
    public function verifyDissolveLog(){
//        remark: remark,log_id:log_id,verify_type:verify_type,is_poundage:is_poundage
        $remark = I('remark');
        $log_id = I('log_id');
        $verify_type = I('verify_type');
        $is_poundage = I('is_poundage');
        if(empty($remark)){
            $this->ajaxReturn(array(Constants::SUCCESS=>false,Constants::MESSAGE=>'请填写备注信息!'));
        }
        if(empty($log_id)){
            $this->ajaxReturn(array(Constants::SUCCESS=>false,Constants::MESSAGE=>'所选记录不能为空!'));
        }
        if(empty($verify_type)){
            $this->ajaxReturn(array(Constants::SUCCESS=>false,Constants::MESSAGE=>'审核类型不能为空!'));
        }
        if($verify_type == 1 && $is_poundage == ''){
            $this->ajaxReturn(array(Constants::SUCCESS=>false,Constants::MESSAGE=>'请选择是否需要手续费!'));
        }
        $logic = new RoomLogic();
        $status = $logic->verifyDissolveLog($remark,$log_id,$verify_type,$is_poundage);
        switch ($status){
            case 0:
                $this->ajaxReturn(array(Constants::SUCCESS=>false,Constants::MESSAGE=>'该记录已经审核过!'));
                break;
            case 1:
                if($verify_type == 1){
                    $message = '通过审核成功！';
                }else{
                    $message = '不通过审核成功！';
                }
                $this->ajaxReturn(array(Constants::SUCCESS=>true,Constants::MESSAGE=>$message));
                break;
            case 2:
                $this->ajaxReturn(array(Constants::SUCCESS=>false,Constants::MESSAGE=>'审核失败!'));
                break;
            case 3:
                $this->ajaxReturn(array(Constants::SUCCESS=>false,Constants::MESSAGE=>'该擂主钻石不足!'));
                break;
            case 4:
                $this->ajaxReturn(array(Constants::SUCCESS=>false,Constants::MESSAGE=>'对不起，当前房间不能解散!'));
                break;
        }
    }


    public function dissolveLog(){
        $model = D('DissolveLog');
        //构建查询条件
        $pagesize = 20;
        $user_id = I('user_id');
        $room_id = I('room_id');
        $pagenumber = I('pagenum', '');
        $startTime = I('startTime', '');
        $endTime = I('endTime', '');
        if (empty($pagenumber) || $pagenumber <= 0)
            $pagenumber = 1;
        if ($user_id != '') {
            $map['t_dissolve_log.user_id'] = $user_id;
        }
        if ($startTime != null && $startTime != '' && $endTime != null && $endTime != '') {
            $map['t_dissolve_log.create_time'] = array(array('gt', strtotime($startTime)), array('lt', strtotime($endTime)));
        }
        //查询
        $startno = ($pagenumber - 1) * $pagesize;
        $limit = $startno . ',' . $pagesize;//每页的数据数和内容$limit
        $list = $model->where($map)->order('id desc')->limit($limit)
            ->field('t_dissolve_log.*')->select();
        foreach ($list as $key => $value) {
            $list[$key]['create_time'] = date("Y-m-d H:i:s", $list[$key]['create_time']);
        }
        $recordcount = $model->where($map)->count();
        //封装分页对象
        $page = new Page($pagenumber, $recordcount, $pagesize);
        $page = (array)$page;//转换为数组

        $this->bcItemPush('房间解散记录申请列表', U('Log/dissolveLog'));

        //显示数据
        $this->assign('startTime', $startTime);
        $this->assign('endTime', $endTime);
        $this->assign('page', $page);
        $this->assign('user_id', $user_id);
        $this->assign('room_id', $room_id);
        $this->assign('pagenumber', $pagenumber);
        $this->assign('list', $list);
        $this->assign('recordcount', $recordcount);
        $this->display('dissolveLog');
    }

    public function orderList(){
        $model = D('Order');
        //构建查询条件
        $pagesize = 20;
        $user_id = I('user_id');
        $goods_id = I('goods_id');
        $status = I('status');
        $pagenumber = I('pagenum', '');
        $startTime = I('startTime', '');
        $endTime = I('endTime', '');
        if (empty($pagenumber) || $pagenumber <= 0)
            $pagenumber = 1;
        if ($user_id != '') {
            $map['t_order.user_id'] = $user_id;
        }
        if ($goods_id != '') {
            $map['t_order.goods_id'] = $goods_id;
        }
        if ($status != '' && $status != -1) {
            $map['t_order.status'] = $status;
        }
        if ($startTime != null && $startTime != '' && $endTime != null && $endTime != '') {
            $map['t_order.create_time'] = array(array('gt', strtotime($startTime)), array('lt', strtotime($endTime)));
        }
        //查询
        $startno = ($pagenumber - 1) * $pagesize;
        $limit = $startno . ',' . $pagesize;//每页的数据数和内容$limit
        $list = $model->join('LEFT JOIN t_goods g ON g.id=t_order.goods_id')
            ->join('LEFT JOIN t_user u ON u.id=t_order.user_id')
            ->where($map)->order('id desc')->limit($limit)
            ->field('t_order.*,g.title,u.wechat_number')->select();

        foreach ($list as $key => $value) {
            $list[$key]['create_time'] = date("Y-m-d H:i:s", $list[$key]['create_time']);
        }
        $recordcount = $model->where($map)->count();
        $countMoney = $this->countMoney($map);
        //封装分页对象
        $page = new Page($pagenumber, $recordcount, $pagesize);
        $page = (array)$page;//转换为数组

        $this->bcItemPush('充值卡领取记录', U('Log/orderList'));

        //显示数据
        $this->assign('startTime', $startTime);
        $this->assign('endTime', $endTime);
        $this->assign('page', $page);
        $this->assign('user_id', $user_id);
        $this->assign('countMoney', $countMoney);
        $this->assign('goods_id', $goods_id);
        $this->assign('status', $status);
        $this->assign('pagenumber', $pagenumber);
        $this->assign('list', $list);
        $this->assign('recordcount', $recordcount);
        $this->display('orderList');
    }

    private function countMoney($map){
        $result = D('Order')->where($map)->field('sum(money) as total_money,sum(poundage) as total_poundage,sum(total_money) as total_integral')
            ->select();
        return $result[0];
    }

    public function verifyOrder(){
        $log_id = I('log_id');
        if(empty($log_id)){
            $this->ajaxReturn(array(Constants::SUCCESS=>false,Constants::MESSAGE=>'无法审核该记录!'));
        }
        $logic = new OrderLogic();
        $status = $logic->verify($log_id);
        switch ($status){
            case 0:
                $this->ajaxReturn(array(Constants::SUCCESS=>false,Constants::MESSAGE=>'该记录已经审核过!'));
                break;
            case 1:
                $message = '通过审核成功！';
                $this->ajaxReturn(array(Constants::SUCCESS=>true,Constants::MESSAGE=>$message));
                break;
            case 2:
                $this->ajaxReturn(array(Constants::SUCCESS=>false,Constants::MESSAGE=>'审核失败!'));
                break;
        }
    }

    /**
     * 查看用户中奖记录
     */
    public function lotteryLog(){
        $model = D('LotteryLog');
        //构建查询条件
        $pagesize = 20;
        $id = I('id');
        $user_id = I('user_id');
        $prize_id = I('prize_id');
        $status = I('status');
        $type = I('type');
        $pagenumber = I('pagenum', '');
        $startTime = I('startTime', '');
        $endTime = I('endTime', '');
        if (empty($pagenumber) || $pagenumber <= 0)
            $pagenumber = 1;
        if ($user_id != '') {
            $map['t_lottery_log.user_id'] = $user_id;
        }
        if ($id != '') {
            $map['t_lottery_log.id'] = $id;
        }
        if ($prize_id != '') {
            $map['t_lottery_log.prize_id'] = $prize_id;
        }
        if ($status != '' && $status != -1) {
            $map['t_lottery_log.status'] = $status;
        }
        if ($type != '' && $type != -1) {
            $map['t_lottery_log.type'] = $type;
        }
        if ($startTime != null && $startTime != '' && $endTime != null && $endTime != '') {
            $map['t_lottery_log.create_time'] = array(array('gt', strtotime($startTime)), array('lt', strtotime($endTime)));
        }
        //查询
        $startno = ($pagenumber - 1) * $pagesize;
        $limit = $startno . ',' . $pagesize;//每页的数据数和内容$limit
        $list = $model
            ->join('LEFT JOIN t_user u ON u.id=t_lottery_log.user_id')
            ->where($map)->order('id desc')->limit($limit)
            ->field('t_lottery_log.*,u.wechat_number')->select();
        foreach ($list as $key => $value) {
            $list[$key]['create_time'] = date("Y-m-d H:i:s", $list[$key]['create_time']);
        }
        $recordcount = $model->where($map)->count();
        //封装分页对象
        $page = new Page($pagenumber, $recordcount, $pagesize);
        $page = (array)$page;//转换为数组

        $this->bcItemPush('中奖记录', U('Log/lotteryList'));

        //显示数据
        $this->assign('startTime', $startTime);
        $this->assign('endTime', $endTime);
        $this->assign('page', $page);
        $this->assign('user_id', $user_id);
        $this->assign('id', $id);
        $this->assign('prize_id', $prize_id);
        $this->assign('status', $status);
        $this->assign('type', $type);
        $this->assign('pagenumber', $pagenumber);
        $this->assign('list', $list);
        $this->assign('recordcount', $recordcount);
        $this->display('lotteryLog');
    }

    public function verifyLotteryLog(){
        $log_id = I('log_id');
        if(empty($log_id)){
            $this->ajaxReturn(array(Constants::SUCCESS=>false,Constants::MESSAGE=>'无法审核该记录!'));
        }
        $logic = new LotteryLgLogic();
        $status = $logic->verify($log_id);
        $success = false;
        switch ($status){
            case 0:
                $message = '该记录已经审核过！';
                break;
            case 1:
                $message = '通过审核成功！';
                $success = true;
                break;
            case 2:
                $message = '审核失败!';
                break;
        }
        $this->ajaxReturn(array(Constants::SUCCESS=>$success,Constants::MESSAGE=>$message));
    }

    /**
     * 跳转到清除场次榜数据界面
     */
    public function updateRankInfoUI(){
        $this->display('updateRank');
    }

    /**
     * ajax清除场次榜数据
     */
    public function ajaxUpdateRankInfo(){
        $level = I('level');
        $type = I('type');
        if($type != Constants::MODEL_TYPE_1V1 && $type != Constants::MODEL_TYPE_3V3 && $type !=  Constants::MODEL_TYPE_5V5){
            $this->ajaxReturn(array(Constants::SUCCESS=>false,Constants::MESSAGE=>'请选择正确的对战类型！'));
        }
        if(empty($level) || !is_numeric($level) || $level<= 0){
            $this->ajaxReturn(array(Constants::SUCCESS=>false,Constants::MESSAGE=>'请填写正确的榜前名次！'));
        }
        $logic = new MatchLogic();
        $result = $logic->clearRankInfo($level,$type);
        $success = false;
        $message = '';
        switch ($result['status']){
            case 0:
                $message = '清除失败！';
                $data = $result['error'];
                break;
            case 1:
                $message = '清除成功！';
                $success = true;
                break;
            case 2:
                $message = '请前往系统设置修改正确的场次榜上榜所需场次!';
                break;
        }
        $this->ajaxReturn(array(Constants::SUCCESS=>$success,Constants::MESSAGE=>$message,'data'=>$data));
    }

    /**
     * 1v1的场次榜
     */
    public function rank(){
        $model = D('Ranking');
        $pagesize = 20;
        $user_id = I('user_id');
        $pagenumber = I('pagenum', '');
        if (empty($pagenumber) || $pagenumber <= 0)
            $pagenumber = 1;
        $startno = ($pagenumber - 1) * $pagesize;
        $limit = $startno . ',' . $pagesize;//每页的数据数和内容$limit
        $map['t_ranking.type'] = Constants::MODEL_TYPE_1V1;
        if(!empty($user_id)){
            $map['t_ranking.user_id'] = $user_id;
        }
        $userList = D('Ranking')
            ->join('LEFT JOIN t_user u ON u.id=t_ranking.user_id')
            ->field('u.nickname,u.phone,t_ranking.user_id,t_ranking.id,t_ranking.total_number,t_ranking.win_number')
            ->where($map)
            ->order('t_ranking.total_number desc,t_ranking.user_id asc')->limit($limit)->select();
        foreach ($userList as $key=>$value){
            $where['total_number'] = array('gt',$value['total_number']);
            $userList[$key]['rank'] = D('Ranking')->where($where)->count() + 1;
        }
        $recordcount = $model->where($map)->count();
        //封装分页对象
        $page = new Page($pagenumber, $recordcount, $pagesize);
        $page = (array)$page;//转换为数组
        $this->assign('page', $page);
        $this->assign('user_id', $user_id);
        $this->assign('pagenumber', $pagenumber);
        $this->assign('list', $userList);
        $this->assign('recordcount', $recordcount);
        $this->display('rank');
    }

    /**
     * 更新字段
     */
    public function updateField()
    {
        $id = I('id');
        $value = I('value');
        $field = I('field');
        $table = I('table');
        $where['id'] = ':id';
        $bind[':id'] = $id;
        $model = M();
        $result = $model->table($table)->where($where)->bind($bind)->setField($field,$value);
        if($result){
            $this->ajaxReturn(array('success'=>true,'message'=>'修改成功'));
        }else{
            $this->ajaxReturn(array('success'=>false,'message'=>'修改失败'));
        }
    }
}